<?
/*
 * emarket - a website to create your own online-shop.  
 * Copyright (C) 2009  Frédéric Delorme
 *
 * This file is part of emarket.
 *
 * emarket is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * emarket is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with emarket.  If not, see <http://www.gnu.org/licenses/>.
 *
 * To contact me: frederic.delorme@gmail.com
 * to see some information on te project : http://code.google.col/p/os-emarket/
 *
 */

/**
 * Template rendering system.
 * @author frederic
 *
 */
class Template{

	private static $_instance;
	private $active = "";
	private $parameters=array(''=>"");
	
	function __construct(){
		$config = Configuration::getInstance();
		$i18n = I18n::getInstance();
		$active_language = $i18n->getLanguage();
		$this->active = $config->getParam("theme","active","");
		if($this->active==""){
			__warn("theme/active key in config file is not defined: set to default theme.");
			$this->active = "default";
		}
		// Verify if theme provides some specific translated text.
		$theme_I18n=dirname($_SERVER['SCRIPT_FILENAME'])."/webapp/themes/".$this->active."/i18n/".$active_language."/";
		__debug(__CLASS__,"::__construct() - retrieve language file from: '$theme_I18n'",__FILE__,__LINE__);
		if (file_exists($theme_I18n)){
			$items = opendir($theme_I18n);
			while (($file = readdir($items)) !== false ) {
				if(!is_dir($file)
					&& $file!="." 
					&& $file!=".."
					&& $file !="thumbs.db"){
						__debug(__CLASS__,"::__construct() - load file : '$file' => messages",__FILE__,__LINE__);
						$messages = parse_ini_file($theme_I18n.$file,true);
						$i18n->addTemplateMessage($messages);
				}
			}
		}
	}
	public function getInstance(){
		if(empty(self::$_instance)){
			self::$_instance = new Template();
		}
		return self::$_instance;
	}
	/**
	 * Add a parameter to the template renderer.
	 * @param $key
	 * @param $value
	 */
	public function setParam($key,$value){
		$this->parameters[$key]=serialize($value);
	}
	/**
	 * Return the requested parameter $key
	 * @param $key
	 * @return value of the parameter (moslty a string)
	 */
	public function getParam($key){
		return unserialize($this->parameters[$key]);
	}
	
	public function getActive(){
		return $this->active;
	}
	public function setActive($name){
		$this->active=$name;
	}
	
	/**
	 * Return the completed path for a "themified" styles sheet.
	 * @param $name Name of the css file without extension.
	 * @return String containing site relative path of the stylesheet.
	 */
	public function getCss($name){
		return "webapp/themes/".$this->getActive()."/public/css/".$name.".css";
	}
	public function setMaster($master="main"){
		$this->master=$master;	
	}
	
	/**
	 * Create the script html tag to import all the javascript library listed into the 
	 * "javascript/api" parameter from config.ini file.
	 */
	public function importJs(){
		$cssfiles = __param("javascript","api");
		$list=split(",",$cssfiles);
		$htmlcss = "";
		foreach($list as $item){
			$htmlcss.="<script src=\"webapp/public/js/$item\" type=\"text/javascript\"></script>\n";
		}
		echo $htmlcss;
	}
	
	/**
	 * create the url part for an action call.
	 * @param $manager
	 * @param $action
	 * @param $type
	 * @param $id
	 * @return String containing the url to action controller.
	 */
	public function link($manager, $action,$type,$id="*"){
		$url = "/$manager/$action/$type/$id";
		if(__param_is_activated("system","rewrite")){
			$url = "?".$url;
		}
		return $url;
	}
}
?>